/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.common.utils;

import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.zzr.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 * <p>
 * 作为一个单例使用
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashObjOperations;
    //NOTEME 没有这种类型，只能使用上面这种！
//    @Autowired
//    private HashOperations<String, String, String> hashStringOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    /**
     * 默认过期时长，单位：秒
     */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1;
    //NOTEME Date 会转为时间戳
    private final static Gson gson = new Gson();


    public void set(String key, Object value, long expire) {
        valueOperations.set(key, toJson(value));
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    //region 封装其他类型 by zzr

    /**
     * HMSET
     *
     * @param key
     * @param map
     */
    public void hMSet(String key, Map<String, Object> map) {
        hashObjOperations.putAll(key, map);
    }

    /**
     * 往 hash 增加一个
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hSet(String key, String hashKey, Object value) {
        hashObjOperations.put(key, hashKey, value);
    }

    /**
     * error!
     * @param key
     * @param stringMap
     */
//    public void hMSet(String key, Map<String, String> stringMap) {
//        hashStringOperations.putAll(key, stringMap);
//    }


    /**
     * 获取 Hash 中的value （转为对象后）
     *
     * @param key
     * @param fieldName
     * @return
     */
    public <T> T hGet(String key, String fieldName, Class<T> clazz) {
        Object value = hashObjOperations.get(key, fieldName);
        return value == null ? null : fromJson(value.toString(), clazz);
    }

    public <T> T hGet(String key, String fieldName, TypeReference<T> typeReference) {
        Object value = hashObjOperations.get(key, fieldName);
        return value == null ? null : fromJson(value.toString(), typeReference);
    }

    /**
     * 获取数据转为 String
     *
     * @param key
     * @param fieldName
     * @return
     */
    public String hGet(String key, String fieldName) {
        Object value = hashObjOperations.get(key, fieldName);
        return value == null ? null : value.toString();
    }


    //region SET


    //endregion

    //endregion


    /**
     * Object转成JSON数据
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String) {
            return String.valueOf(object);
        }
        //return gson.toJson(object);
        return JsonUtils.parseString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }

    private <T> T fromJson(String json, TypeReference<T> typeReference) {
        return JsonUtils.parseEntity(json, typeReference);
    }
}
